﻿using DAL.comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public  class ProductDAL
    {
        private SqlCommand cmd;
        public ProductDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
        }
        public DataSet CategoryList()
        {
            cmd.CommandText = "SelectCategoryList";
            cmd.Parameters.Clear();
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        public bool ForEdit(SqlCommand cmd)
        {
            try
            {
                int num = SQLHelper.ExecuteSQL(cmd);
                if (num > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 加载存储过程参数方法实现
        /// </summary>
        /// <param name="bo"></param>

        private void SetPara(Product bo)
        {
           
            cmd.Parameters.Add("@ProductID", SqlDbType.NVarChar, 6).Value = bo.ProductID;
            cmd.Parameters.Add("@ProductName", SqlDbType.NVarChar, 80).Value = bo.ProductName;
            cmd.Parameters.Add("@SpellingCode", SqlDbType.NVarChar, 40).Value = bo.SpellingCode;
            cmd.Parameters.Add("@BarCode", SqlDbType.NVarChar, 14).Value = bo.BarCode;
            cmd.Parameters.Add("@Special", SqlDbType.NVarChar, 40).Value = bo.Special;
            cmd.Parameters.Add("@Unit", SqlDbType.NVarChar, 6).Value = bo.Unit;
            cmd.Parameters.Add("@Origin", SqlDbType.NVarChar, 50).Value = bo.Origin;
            cmd.Parameters.Add("@CategoryID", SqlDbType.NVarChar, 6).Value = bo.Category .CategoryID ;
            cmd.Parameters.Add("@PurchasePrice", SqlDbType.Decimal, 18).Value = bo.PurchasePrice;
            cmd.Parameters.Add("@SalePrice", SqlDbType.Decimal, 18).Value = bo.SalePrice;
            cmd.Parameters.Add("@Quantity", SqlDbType.Int ).Value = bo.Quantity ;






        }
        /// <summary>
        /// 新增数据方法实现
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        public bool Insert(Product bo)
        {
            cmd.CommandText = "InsertProduct";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }/// <summary>
         /// 数据修改方法
         /// </summary>
         /// <param name="bo"></param>
         /// <returns></returns>
        public bool Update(Product bo)
        {
            cmd.CommandText = "UpdateProduct";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }/// <summary>
         /// 数据删除方法
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
        public bool Delete(Product bo)
        {
            cmd.CommandText = "DeleteProduct";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }
        /// <summary>
        /// 根据产品类别查询产品类别编号
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        //public Category FindCategoryIDByCategoryName(string name)
        //{
        //    cmd.CommandText = "FindCategoryIDByCategoryName";
        //    cmd.Parameters.Clear();
        //    cmd.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 6).Value = name ;
        //    SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
        //    Category bo = new Category();
        //    while (read.Read())
        //    {
        //        bo = GetValueByDataRead1(read);//调用GetValueByDataRead方法将数据库读取到对象中
        //    }
        //    read.Close();//关闭读库
        //    return bo;
        //}
        /// <summary>
        /// 将数据库读库结果返回到实体中
        /// </summary>
        /// <param name="read">读库结果</param>
        /// <returns>Customer业务实体bo</returns>
        private static Product GetValueByDataRead(SqlDataReader read)
        {
            Product bo = new Product();
            bo.ProductID = read["ProductID"].ToString().Trim();
            bo.ProductName = read["ProductName"].ToString().Trim();
            bo.SpellingCode = read["SpellingCode"].ToString().Trim();
            bo.BarCode = read["BarCode"].ToString().Trim();
            bo.Special = read["Special"].ToString().Trim();
            bo.Unit = read["Unit"].ToString().Trim();
            bo.Origin = read["Origin"].ToString().Trim();
            bo.CategoryID = read["CategoryID"].ToString().Trim();
            bo.PurchasePrice =  decimal.Parse(read["PurchasePrice"].ToString().Trim());
            bo.SalePrice = decimal.Parse (read["SalePrice"].ToString().Trim());
            bo.Quantity = int.Parse (read["Quantity"].ToString().Trim());
            return bo;
        }
        //private static Category GetValueByDataRead1(SqlDataReader read)
        //{
        //    Category bo = new Category();
        //    bo.CategoryID  = read["CategoryID"].ToString().Trim();
        //    return bo;
        //}

        /// <summary>
        /// 根据传入的ID查找对应记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product FindByID(string id)
        {
            cmd.CommandText = "FindProductByID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@ProductID", SqlDbType.NVarChar, 6).Value = id;
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            Product bo = new Product();
            while (read.Read())
            {
                bo = GetValueByDataRead(read);//调用GetValueByDataRead方法将数据库读取到对象中
            }
            read.Close();//关闭读库
            return bo;
        }
        public List<Product> GetList()
        {
            cmd.CommandText = "SelectProductList";//指定存储过程名称
            cmd.Parameters.Clear();//清空存储过程参数列表
            List<Product> list = new List<Product>();//定义泛型集合用于存储数据库返回数据
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            foreach (var item in read)
            {
                Product c = GetValueByDataRead(read);
                 list.Add(c);
            }
            read.Close();//关闭读库
            return list;
        }


    }
}
